﻿using OF.Lib.DataAccess.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace OF.Lib.DataAccess.Core
{
    public static class DBConfigHelper
    {
        static DBConfig m_DbConfig = null;

        private static object _obj = new object();
        public static DBConfig GetDbConfig()
        {
            lock (_obj)
            {
                if (m_DbConfig == null)
                {
                    m_DbConfig = LoadDbConfig();
                }
            }
            return m_DbConfig;
        }

        public static DBGroup GetDBGroup(string connKey)
        {
            DBConfig dbcfg = GetDbConfig();
            DBGroup group = dbcfg.DBGroupList.Find(f => f.ConnKey.Trim().ToUpper() == connKey.Trim().ToUpper());
            if (group == null)
            {
                throw new Exception(string.Format("Don't found ConnKey \"{0}\" in db.config!", connKey));
            }
            return group;
        }

        private static DBConfig LoadDbConfig()
        {
            List<string> connIdList = new List<string>();
                       
            string filePath = Path.Combine(GlobalDefine.ConfigurationPath, "DB.config");
            if (File.Exists(filePath))
            {
                DBConfig config = XMLHelper.LoadFromXml<DBConfig>(filePath);

                for (int i = 0; i < config.DBGroupList.Count; i++)
                {
                    string connKey1 = config.DBGroupList[i].ConnKey.Trim().ToUpper();
                    for (int j = i + 1; j < config.DBGroupList.Count; j++)
                    {
                        string connKey2 = config.DBGroupList[j].ConnKey.Trim().ToUpper();
                        if (connKey1 == connKey2)
                        {
                            throw new Exception(string.Format("DB.config文件中，DBGroup的ConnKey \"{0}\"有重复！", connKey1));
                        }
                    }

                    DBGroup dbGroup = config.DBGroupList[i];
                    List<DBConn> masterConnList = dbGroup.DBConnList.FindAll(f => f.MasterDB == true);
                    if (masterConnList == null || masterConnList.Count != 1)
                    {
                        throw new Exception(string.Format("DB.config文件中，DBGroup的ConnKey为\"{0}\"的节点，必须有且只能有一个MasterDB=\"true\"的配置属性！", connKey1));
                    }

                    foreach(DBConn conn in dbGroup.DBConnList)
                    {
                        if(string.IsNullOrWhiteSpace(conn.Id))
                        {
                            throw new Exception(string.Format("DB.config文件中，DbGroup\"{0}\"中有DbConn的Id为空！", dbGroup.ConnKey));
                        }

                        if(connIdList.Contains(conn.Id.Trim().ToUpper()))
                        {
                            throw new Exception(string.Format("DB.config文件中，DbConn的Id\"{0}\"有重复！", conn.Id));
                        }
                        else
                        {
                            connIdList.Add(conn.Id.Trim().ToUpper());
                        }
                    }

                }



                for (int i = 0; i < config.SQLFileList.Count; i++)
                {
                    string sqlFile = config.SQLFileList[i].ToUpper().Trim();
                    for (int j = i + 1; j < config.SQLFileList.Count; j++)
                    {
                        string sqlFile2 = config.SQLFileList[j].ToUpper().Trim();
                        if (sqlFile == sqlFile2)
                        {
                            throw new Exception(string.Format("DB.config文件中，SQLFile \"{0}\"有重复！", sqlFile));
                        }
                    }
                }

                return config;
            }
            else
            {
                throw new Exception(string.Format("Not found sql file {0}", filePath));
            }

        }




    }

}